package com.imooc.service.impl;

import com.imooc.mapper.UsersMapper;
import com.imooc.pojo.Users;
import com.imooc.pojo.bo.UserBO;
import com.imooc.service.UserService;
import imooc.enums.Sex;
import imooc.utils.DateUtil;
import imooc.utils.MD5Utils;
import org.n3r.idworker.Sid;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Example;

import java.util.Date;

@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private UsersMapper usersMapper;

    @Autowired
    private Sid sid;

    private static final String USER_FACE = "https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1601984294831&di=baf73c806ed792d15e34ddf2e96b1c0f&imgtype=0&src=http%3A%2F%2Fb-ssl.duitang.com%2Fuploads%2Fitem%2F201603%2F06%2F20160306204517_i4Se8.jpeg";

    /**
     * 查询用户是否存在
     * */
    @Transactional(propagation = Propagation.SUPPORTS, rollbackFor = Exception.class)
    @Override
    public boolean queryUsernameIsExist(String username) throws Exception{
        Example example = new Example(Users.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("username", username);
        Users users = usersMapper.selectOneByExample(example);
        return null == users ? false : true;
    }


    /**
     * 创建用户
     * */
    @Override
    @Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
    public Users createUser(UserBO userBO) throws Exception{
        Users user = new Users();
        String userId = sid.nextShort();
        user.setId(userId);
        user.setUsername(userBO.getUsername());

        // 密码加密
        user.setPassword(MD5Utils.getMD5Str(userBO.getPassword()));

        // 设置用户昵称同用户名
        user.setNickname(userBO.getUsername());
        // 默认头像
        user.setFace(USER_FACE);
        // 设置生日
        user.setBirthday(DateUtil.stringToDate("1970-01-01"));
        // 默认性别为保密
        user.setSex(Sex.secret.type);
        // 创建日期
        user.setCreatedTime(new Date());
        // 设置更新日期
        user.setUpdatedTime(new Date());
        int effectRows = usersMapper.insert(user);
        if (effectRows <= 0) {
//            throw new Exception("添加用户失败");
            return null;
        }
        return user;
    }

    /**
     * 用户登录
     * 检索用户名和密码是否匹配，用于登录
     * */
    @Override
    @Transactional(propagation = Propagation.SUPPORTS, rollbackFor = Exception.class)
    public Users queryUserForLogin(String username, String password) throws Exception{
        Example example = new Example(Users.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("username", username);
        criteria.andEqualTo("password", password);
        Users result = usersMapper.selectOneByExample(example);
        return result;
    }
}
